home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / CHSet.ccP < prev    next >
Text File  |  1992-04-14  |  6KB  |  272 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include "<T>.CHSet.h"
  23.  
  24. // A CHSet is implemented as an array (tab) of buckets, each of which
  25. // contains a pointer to a list of <T>CHNodes.  Each node contains a
  26. // pointer to the next node in the list, and a pointer to the <T>.
  27. // The end of the list is marked by a next node pointer which is odd
  28. // when considered as an integer (least significant bit = 1).  The
  29. // assumption is that CHNodes will all begin on even addresses.  If
  30. // the odd pointer is right-shifted by one bit, it becomes the index
  31. // within the tab array of the next bucket (that is, bucket i has
  32. // next bucket pointer 2*(i+1)+1).
  33.  
  34. // The bucket pointers are initialized by the constructor and
  35. // used to support the next(Pix&) method.
  36.  
  37. // This implementation is not portable to machines with different
  38. // pointer and integer sizes, or on which CHNodes might be aligned on
  39. // odd byte boundaries, but allows the same pointer to be used for
  40. // chaining within a bucket and to the next bucket.
  41.  
  42.  
  43. static inline int goodCHptr(<T>CHNode* t)
  44. {
  45.   return ((((unsigned)t) & 1) == 0);
  46. }
  47.  
  48. static inline <T>CHNode* index_to_CHptr(int i)
  49. {
  50.   return (<T>CHNode*)((i << 1) + 1);
  51. }
  52.  
  53. static inline int CHptr_to_index(<T>CHNode* t)
  54. {
  55.   return ( ((unsigned) t) >> 1);
  56. }
  57.  
  58. <T>CHSet::<T>CHSet(unsigned int sz)
  59. {
  60.   tab = (<T>CHNode**)(new <T>CHNodePtr[size = sz]);
  61.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  62.   count = 0;
  63. }
  64.  
  65. <T>CHSet::<T>CHSet(<T>CHSet& a)
  66. {
  67.   tab = (<T>CHNode**)(new <T>CHNodePtr[size = a.size]);
  68.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  69.   count = 0;
  70.   for (Pix p = a.first(); p; a.next(p)) add(a(p));
  71. }
  72.  
  73.  
  74. Pix <T>CHSet::seek(<T&> key)
  75. {
  76.   unsigned int h = <T>HASH(key) % size;
  77.  
  78.   for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
  79.     if (<T>EQ(key, t->hd))
  80.       return Pix(t);
  81.  
  82.   return 0;
  83. }
  84.  
  85.  
  86. Pix <T>CHSet::add(<T&> item)
  87. {
  88.   unsigned int h = <T>HASH(item) % size;
  89.  
  90.   for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
  91.     if (<T>EQ(item, t->hd))
  92.       return Pix(t);
  93.  
  94.   ++count;
  95.   t = new <T>CHNode(item, tab[h]);
  96.   tab[h] = t;
  97.   return Pix(t);
  98. }
  99.  
  100.  
  101. void <T>CHSet::del(<T&> key)
  102. {
  103.   unsigned int h = <T>HASH(key) % size;
  104.  
  105.   <T>CHNode* t = tab[h]; 
  106.   <T>CHNode* trail = t;
  107.   while (goodCHptr(t))
  108.   {
  109.     if (<T>EQ(key, t->hd))
  110.     {
  111.       if (trail == t)
  112.         tab[h] = t->tl;
  113.       else
  114.         trail->tl = t->tl;
  115.       delete t;
  116.       --count;
  117.       return;
  118.     }
  119.     trail = t;
  120.     t = t->tl;
  121.   }
  122. }
  123.  
  124.  
  125. void <T>CHSet::clear()
  126. {
  127.   for (unsigned int i = 0; i < size; ++i)
  128.   {
  129.     <T>CHNode* p = tab[i];
  130.     tab[i] = index_to_CHptr(i+1);
  131.     while (goodCHptr(p))
  132.     {
  133.       <T>CHNode* nxt = p->tl;
  134.       delete(p);
  135.       p = nxt;
  136.     }
  137.   }
  138.   count = 0;
  139. }
  140.  
  141. Pix <T>CHSet::first()
  142. {
  143.   for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
  144.   return 0;
  145. }
  146.  
  147. void <T>CHSet::next(Pix& p)
  148. {
  149.   if (p == 0) return;
  150.   <T>CHNode* t = ((<T>CHNode*)p)->tl;
  151.   if (goodCHptr(t))
  152.     p = Pix(t);
  153.   else
  154.   {
  155.     for (unsigned int i = CHptr_to_index(t); i < size; ++i) 
  156.     {
  157.       if (goodCHptr(tab[i]))
  158.       {
  159.         p =  Pix(tab[i]);
  160.         return;
  161.       }
  162.     }
  163.     p = 0;
  164.   }
  165. }
  166.  
  167. int <T>CHSet::operator == (<T>CHSet& b)
  168. {
  169.   if (count != b.count)
  170.     return 0;
  171.   else
  172.   {
  173.     <T>CHNode* p;
  174.     for (unsigned int i = 0; i < size; ++i)
  175.       for (p = tab[i]; goodCHptr(p); p = p->tl)
  176.         if (b.seek(p->hd) == 0)
  177.           return 0;
  178.     for (i = 0; i < b.size; ++i)
  179.       for (p = b.tab[i]; goodCHptr(p); p = p->tl)
  180.         if (seek(p->hd) == 0)
  181.           return 0;
  182.     return 1;
  183.   }
  184. }
  185.  
  186. int <T>CHSet::operator <= (<T>CHSet& b)
  187. {
  188.   if (count > b.count)
  189.     return 0;
  190.   else
  191.   {
  192.     for (unsigned int i = 0; i < size; ++i)
  193.       for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl)
  194.         if (b.seek(p->hd) == 0)
  195.           return 0;
  196.     return 1;
  197.   }
  198. }
  199.  
  200. void <T>CHSet::operator |= (<T>CHSet& b)
  201. {
  202.   if (&b == this || b.count == 0)
  203.     return;
  204.   for (unsigned int i = 0; i < b.size; ++i)
  205.     for (<T>CHNode* p = b.tab[i]; goodCHptr(p); p = p->tl)
  206.       add(p->hd);
  207. }
  208.  
  209. void <T>CHSet::operator &= (<T>CHSet& b)
  210. {
  211.   for (unsigned int i = 0; i < size; ++i)
  212.   {
  213.     <T>CHNode* t = tab[i]; 
  214.     <T>CHNode* trail = t;
  215.     while (goodCHptr(t))
  216.     {
  217.       <T>CHNode* nxt = t->tl;
  218.       if (b.seek(t->hd) == 0)
  219.       {
  220.         if (trail == tab[i])
  221.           trail = tab[i] = nxt;
  222.         else
  223.           trail->tl = nxt;
  224.         delete t;
  225.         --count;
  226.       }
  227.       else
  228.         trail = t;
  229.       t = nxt;
  230.     }
  231.   }
  232. }
  233.  
  234. void <T>CHSet::operator -= (<T>CHSet& b)
  235. {
  236.   for (unsigned int i = 0; i < size; ++i)
  237.   {
  238.     <T>CHNode* t = tab[i]; 
  239.     <T>CHNode* trail = t;
  240.     while (goodCHptr(t))
  241.     {
  242.       <T>CHNode* nxt = t->tl;
  243.       if (b.seek(t->hd) != 0)
  244.       {
  245.         if (trail == tab[i])
  246.           trail = tab[i] = nxt;
  247.         else
  248.           trail->tl = nxt;
  249.         delete t;
  250.         --count;
  251.       }
  252.       else
  253.         trail = t;
  254.       t = nxt;
  255.     }
  256.   }
  257. }
  258.  
  259. int <T>CHSet::OK()
  260. {
  261.   int v = tab != 0;
  262.   int n = 0;
  263.   for (unsigned int i = 0; i < size; ++i)
  264.   {
  265.     for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n;
  266.     v &= CHptr_to_index(p) == i + 1;
  267.   }
  268.   v &= count == n;
  269.   if (!v) error("invariant failure");
  270.   return v;
  271. }
  272.